<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
    public  Map setExpert(Message msg ,String id  ,Integer uid ,Integer hospital ){
        String selectContent = "new map(id as id , expertId as expert_id, name as name, avatar as avatar, title as title, teachTitle as teach_title, time as time, specialty as specialty, content as content)";
        List<Map> experts = QBuilder.db("basedb").select(selectContent).from("Expert").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("expertId=", id)).query().getResultObj();
        if(experts == null || experts.size() ==0 ){
            msg =  new Message(30041,"没有专家信息");
            msg.put("data",new HashMap());
            return null;
        }
        Map expert =  experts.get(0) ;

        String oid = id;
        // 查询是否关注
        if (experts.size() > 0) {
            Map map = experts.get(0);
            String content = Tools.getDesc(map.get("content"));
            org.jsoup.nodes.Document d= Jsoup.parse(content);
            String summary = Tools.getDesc(map.get("summary"), "");
            if ("".equals(summary)) {
                summary = d.body().text();
                if ("".equals(summary))
                    summary = "暂无专家介绍";
                expert.put("summary", summary);
            }
            expert.put("content", content);
            expert.put("specialty", Tools.getDesc(map.get("specialty"),""));
            expert.put("teach_title", Tools.getDesc(map.get("teach_title"),""));
            expert.put("title", Tools.getDesc(map.get("title"),""));
            String avatar = Tools.getDesc(map.get("avatar"), "");

            if (!"".equals(avatar)) {
                expert.put("avatar", avatar);
            }
            String time = Tools.getDesc(map.get("time"), "");
            if (!"".equals(time)) {
                expert.put("time", time);
            }
            oid = Tools.getDesc(map.get("id"), "");
        }

        OperateHistory oh = new OperateHistory();
        oh.setUid(uid+"");
        oh.setHospital(hospital);
        oh.setOid(oid);
        oh.setCategory(DOCTOR_CATEGORY);
        query(expert, oh);
        String hName = "";
        List<Map> hospitalNames = QBuilder.db("basedb").select("new map(name as name, parent as parent)").from("Hospital").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE)).query().getResultObj();
        if (!isEmpty(hospitalNames)) {
            Map hospitalName = hospitalNames.get(0);
            String parent = Tools.getDesc(hospitalName.get("parent"), "");
            hName = Tools.getDesc(hospitalName.get("name"), "");
            if (!isEmpty(parent)) {
                List<String> names = QBuilder.db("basedb").select("name").from("Hospital").where(new Where("hospital=", parent).and("status = ", STATUS_ENABLE)).query().getResultObj();
                if (!isEmpty(names)) {
                    hName = Tools.getDesc(names.get(0), "");
                }
            }
        }
        expert.put("hospital_name", hName);
        msg.put("data", expert);
        return expert;
    }
%>
<%
    // 查询医生信息
    String id = input.getDesc("id", "-1");
    Integer uid = input.getInt("uid", 0);
    String branch = input.getDesc("branch", "-1");
    Where where  = new Where();
    if (!"-1".equals(branch)) {
        where.and("branch=", branch);
    }
    Map expert = setExpert( msg , id  , uid , hospital );

    List<Map> expertIds = QBuilder.db("basedb").select("new map(s.doctorId as expert_id,s.deptName as dept_name, s.doctorName as name, s.date as date, s.half as half)").from("Schedule as s").where(where.and("s.doctorId=", id).and("s.registerType=2").and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE)).query().getResultObj();
    if (isEmpty(expertIds)) {
        msg.put("data", new HashMap());
	} else {
        Map<String, String> times = new LinkedHashMap<String, String>();
        for (int i = 0; i < expertIds.size(); i++) {
            Map map = expertIds.get(i);
            String date = Tools.getDesc(map.remove("date"), "");
            String half = Tools.getDesc(map.remove("half"), "");
            //half = "1".equals(half) ? "上午" : ("2".equals(half) ? "下午" : half);
            if ("".equals(date) || "".equals(half)) {
                continue;
            }
            Date d = DateUtil.parseDate(date);
		    String time = DateUtil.formatDateByFormat(d, "EEEE").replaceAll("星期", "周");
            String str = Tools.getDesc(times.get(time), "");
            if (str.contains(half)) {
                continue;
            }
            str += "_" + half;
            times.put(time, str);
        }
        Set<Entry<String, String>> set = times.entrySet();
        String result = "";
		for (Entry<String, String> entry : set) {
			String key = entry.getKey();
			String value = entry.getValue();
            result += key;
            String[] values = value.split("_");
            Arrays.sort(values);
            for(int i = 0; i < values.length; i++) {
                    if ("".equals(values[i])) {
                        continue;
                    }
                    result += ("1".equals(values[i]) ? "上午" : ("2".equals(values[i]) ? "下午" : values[i])) + "、";
            }
            result = result.substring(0, result.length() > 1 ? result.length() - 1 : result.length());
            result += "，";
		}
        result = result.substring(0, result.length() > 1 ? result.length() - 1 : result.length());
        result += "。";
        if(expert == null) {
            expert = expertIds.get(0);
            expert.put("time", result);
            expert.put("avatar", DEFAULT_AVATAR);
            expert.put("id", id);
            expert.put("expert_id", id);
            OperateHistory oh = new OperateHistory();
            oh.setUid(uid+"");
            oh.setHospital(hospital);
            oh.setOid(id);
            oh.setCategory(DOCTOR_CATEGORY);
            query(expert, oh);

            msg.put("data", expert);
        }else{
            expert.put("time", result);
        }



    }
    Tools.encryptPrint(out, msg.toJson());
%>
